$(function() {
	let ue;
	let i=3;
	$(function() {
		ue = UE.getEditor('addContent');
		loadClass($("select[name='newsClassId']"));
		$(".form-modify").on("submit", doAdd);
	});

	/**
	 * 表单的提交事件
	 */
	let time=function fn(){
		setTimeout(function(){
			if(i==0){
				window.location="http://127.0.0.1:8080/news-my.do";
			}
			toastr.success(i+"秒后即将跳转至我发表的文章页面")
			i--;
			fn();
		},1000);
	}
	let doAdd = function() {
		let form = $(this);
		var file=window.location.pathname;
		var s=file.substr(file.lastIndexOf("-")+1);
		s=s.substr(0,s.lastIndexOf("."));
		$.post(
			"/news-modify-end-"+s+".do", 
			form.serializeArray(),
			function(data) {
				if(data.ok) {
					toastr.success("修改成功");
					form.trigger("reset");
					ue.setContent("");
					setTimeout(time,1000);
				} else {
					toastr.error("修改失败：" + data.message);
				}
			}, "json"
		);
		return false;
	}
	let loadClass = function(selectObj, fn = function() {}) {
		let id=selectObj.attr("id");
		$.get(
			"news-class-json.do",
			function(data) {
				$.each(data, function(name, item) {
					$("<option></option>").text(item.newsClassName)
						.val(item.newsClassId).appendTo(selectObj);
				});
				$("select").val(id);
				fn();
			}, "json"
		);
		
	}

});
